<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Go by Example 中文版: 环境变量</title>
    <link rel=stylesheet href="site.css">
  </head>
  <script>
      onkeydown = (e) => {
          
          if (e.key == "ArrowLeft") {
              window.location.href = 'command-line-subcommands';
          }
          
          
          if (e.key == "ArrowRight") {
              window.location.href = 'http-clients';
          }
          
      }
  </script>
  <body>

    <div class="example" id="environment-variables">
      <h2><a href="./">Go by Example 中文版</a>: 环境变量</h2>
      
      <table>
        
        <tr>
          <td class="docs">
            <p><a href="http://zh.wikipedia.org/wiki/%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F"><em>环境变量</em></a>
是一种<a href="http://www.12factor.net/config">向 Unix 程序传递配置信息</a>的常见方式。
让我们来看看如何设置、获取以及列出环境变量。</p>

          </td>
          <td class="code empty leading">
            
          
          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            <a href="http://play.golang.org/p/ORoiF_9h42F"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
          <div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
    <span class="s">&quot;fmt&quot;</span>
    <span class="s">&quot;os&quot;</span>
    <span class="s">&quot;strings&quot;</span>
<span class="p">)</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            
          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>使用 <code>os.Setenv</code> 来设置一个键值对。
使用 <code>os.Getenv</code>获取一个键对应的值。
如果键不存在，将会返回一个空字符串。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre>    <span class="nx">os</span><span class="p">.</span><span class="nx">Setenv</span><span class="p">(</span><span class="s">&quot;FOO&quot;</span><span class="p">,</span> <span class="s">&quot;1&quot;</span><span class="p">)</span>
    <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;FOO:&quot;</span><span class="p">,</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Getenv</span><span class="p">(</span><span class="s">&quot;FOO&quot;</span><span class="p">))</span>
    <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;BAR:&quot;</span><span class="p">,</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Getenv</span><span class="p">(</span><span class="s">&quot;BAR&quot;</span><span class="p">))</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>使用 <code>os.Environ</code> 来列出所有环境变量键值对。
这个函数会返回一个 <code>KEY=value</code> 形式的字符串切片。
你可以使用 <code>strings.SplitN</code> 来得到键和值。这里我们打印所有的键。</p>

          </td>
          <td class="code">
            
          <div class="highlight"><pre>    <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">()</span>
    <span class="k">for</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">e</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Environ</span><span class="p">()</span> <span class="p">{</span>
        <span class="nx">pair</span> <span class="o">:=</span> <span class="nx">strings</span><span class="p">.</span><span class="nx">SplitN</span><span class="p">(</span><span class="nx">e</span><span class="p">,</span> <span class="s">&quot;=&quot;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
        <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">pair</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>

          </td>
        </tr>
        
      </table>
      
      <table>
        
        <tr>
          <td class="docs">
            <p>运行这个程序，显示我们在程序中设置的 <code>FOO</code> 的值，
然而没有设置的 <code>BAR</code> 是空的。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="gp">$</span> go run environment-variables.go
<span class="go">FOO: 1</span>
<span class="go">BAR: </span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>键的列表是由你的电脑情况而定的。</p>

          </td>
          <td class="code leading">
            
          <div class="highlight"><pre><span class="go">TERM_PROGRAM</span>
<span class="go">PATH</span>
<span class="go">SHELL</span>
<span class="go">...</span>
</pre></div>

          </td>
        </tr>
        
        <tr>
          <td class="docs">
            <p>如果我们在运行前设置了 <code>BAR</code> 的值，那么运行程序将会获取到这个值。</p>

          </td>
          <td class="code">
            
          <div class="highlight"><pre><span class="gp">$</span> <span class="nv">BAR</span><span class="o">=</span><span class="m">2</span> go run environment-variables.go
<span class="go">FOO: 1</span>
<span class="go">BAR: 2</span>
<span class="go">...</span>
</pre></div>

          </td>
        </tr>
        
      </table>
      
      
      <p class="next">
        下一个例子: <a href="http-clients">HTTP 客户端</a>
      </p>
      
      <p class="footer">
        <a href="https://twitter.com/mmcgrana">@mmcgrana</a> 编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>      </p>
      </p>
    </div>
    <script>
      var codeLines = [];
      codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A    \"fmt\"\u000A    \"os\"\u000A    \"strings\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push('    os.Setenv(\"FOO\", \"1\")\u000A    fmt.Println(\"FOO:\", os.Getenv(\"FOO\"))\u000A    fmt.Println(\"BAR:\", os.Getenv(\"BAR\"))\u000A');codeLines.push('    fmt.Println()\u000A    for _, e := range os.Environ() {\u000A        pair := strings.SplitN(e, \"=\", 2)\u000A        fmt.Println(pair[0])\u000A    }\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
    </script>
    <script src="site.js" async></script>
  </body>
</html>
